/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ctyon.watchmanager.ui.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.model.db.entity.ChatMessageEntity;
import com.ctyon.watchmanager.databinding.ItemChatInBinding;
import com.ctyon.watchmanager.databinding.ItemChatOutBinding;
import com.ctyon.watchmanager.listener.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ChatViewHolder> {

    List<ChatMessageEntity> mChatMessageEntityList = new ArrayList<>();

    @Nullable
    private final ItemClickListener chatsItemClickListener;

    public ChatAdapter(@Nullable ItemClickListener itemClickListener) {
        chatsItemClickListener = itemClickListener;
        setHasStableIds(true);
    }

    public void setChatList(final List<ChatMessageEntity> chats) {
        mChatMessageEntityList.clear();
        mChatMessageEntityList.addAll(chats);
        notifyDataSetChanged();
    }

    public void addChat(ChatMessageEntity chat) {
        mChatMessageEntityList.add(chat);
        notifyItemInserted(mChatMessageEntityList.size() - 1);
    }

    public void removeChat(int position) {
        mChatMessageEntityList.remove(position);
        notifyDataSetChanged();
    }

    public List<ChatMessageEntity> getChats() {
        return mChatMessageEntityList;
    }

    @Override
    public ChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("Lee_ChatAdapter", "onCreateViewHolder");
        if (viewType == ITEM_TYPE.TYPE_IN_MSG.ordinal()) {
            ItemChatInBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_chat_in, parent, false);
            binding.setListener(chatsItemClickListener);
            return new ChatViewHolder(binding);
        } else {
            ItemChatOutBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_chat_out, parent, false);
            binding.setListener(chatsItemClickListener);
            return new ChatViewHolder(binding);
        }
    }

    @Override
    public void onBindViewHolder(ChatViewHolder holder, int position) {
        Log.d("Lee_ChatAdapter", "onBindViewHolder");
        ChatMessageEntity chatMessageEntity = mChatMessageEntityList.get(position);
        int viewType = getItemViewType(position);
        if (viewType == ITEM_TYPE.TYPE_IN_MSG.ordinal()) {
            holder.inBinding.setMessage(chatMessageEntity);
            holder.inBinding.setIndex(position);
            holder.inBinding.executePendingBindings();
        } else {
            holder.outBinding.setMessage(chatMessageEntity);
            holder.outBinding.setIndex(position);
            holder.outBinding.executePendingBindings();
        }
    }

    @Override
    public int getItemCount() {
        return mChatMessageEntityList == null ? 0 : mChatMessageEntityList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public ChatMessageEntity getItem(int index) {
        return mChatMessageEntityList.get(index);
    }

    //定义一个枚举，分别表示接受消息和发送消息
    public enum ITEM_TYPE {
        TYPE_IN_MSG,
        TYPE_OUT_MSG
    }

    //根据消息的属性返回不同的viewType
    @Override
    public int getItemViewType(int position) {
        if (mChatMessageEntityList.get(position).isInMsg()) {
            return ITEM_TYPE.TYPE_IN_MSG.ordinal();
        } else {
            return ITEM_TYPE.TYPE_OUT_MSG.ordinal();
        }
    }

    static class ChatViewHolder extends RecyclerView.ViewHolder {

        private ItemChatInBinding inBinding;
        private ItemChatOutBinding outBinding;

        public ChatViewHolder(ItemChatInBinding binding) {
            super(binding.getRoot());
            this.inBinding = binding;
        }

        public ChatViewHolder(ItemChatOutBinding binding) {
            super(binding.getRoot());
            this.outBinding = binding;
        }

    }


}
